package cn.com.ccxe.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.core.entity.MonitorType;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.service.KeywordBussiness;
import cn.com.ccxe.service.TypeBussiness;
import cn.com.ccxe.web.form.TypeActionForm;

import com.ccxe.manage.service.CurrUserInfo;

/**
 *  {@link TypeAction#dispatch(HttpServletRequest, HttpServletResponse)}
 *  的分发机处理是根据属性method的值来进行分发。
 *  如：要调用 add方法,只需在表单中添加如下形式的控件<br>
 *  	{@code <input id='method' name='method' value='add'/> }
 * @author Administrator
 *
 */
public class TypeAction extends BasciAction {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(TypeAction.class);
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void dispatch(HttpServletRequest req,
							HttpServletResponse resp)
			throws ServletException,
				   IOException {
		logger.info("--------MonitorTypeAction.dispatch()------");
		TypeActionForm actionForm = new TypeActionForm(req);
		//请求的方法
		String method = actionForm.getMethod();
		if(!StringUtils.isEmpty(method)) {
			method = method.trim();
		}
		CurrUserInfo userInfo = actionForm.getUserInfo();
		if(userInfo == null) {
			logger.info("用户信息已过期，请重新登录！");
			resp.sendRedirect("/login.jsp");
			invalidateSession(req);
			return;
		}
		if(StringUtils.isEmpty(method)) {
			show(req, resp, actionForm);
			return;
		}
		if("show".equals(method)) {
			show(req, resp, actionForm);
			return;
		}
		if("add".equals(method)) {
			add(req, resp, actionForm);
			return;
		}
		if("update".equals(method)) {
			update(req, resp, actionForm);
			return;
		}
		if("del".equals(method)) {
			del(req, resp, actionForm);
			return;
		}
		if("toUpdate".equals(method)) {
			toUpdate(req, resp, actionForm);
			return;
		}
		if("getAllObjectInThis".equals(method)) {
			getAllObjectInThis(req, resp, actionForm);
			return;
		}
	}
	
	private void show(HttpServletRequest req,
						HttpServletResponse resp, 
						TypeActionForm actionForm)
		throws ServletException,
			   IOException {
		logger.info("--------MonitorTypeAction.show()------");
		TypeBussiness bussiness = new TypeBussiness();
		List<MonitorType> monitorTypeList = bussiness.getAllMonitorType();
		req.setAttribute("monitorTypeList",monitorTypeList);
		forward(req, resp, "/zebra/monitor/monitorType/show.jsp");
	}
	
	/**
	 * 增加监控类型，类型名称不能为空。
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest req,
					   HttpServletResponse resp, 
					   TypeActionForm actionForm)
			throws ServletException,
			   	   IOException {
		
logger.info("-----------MonitorTypeAction.add()-----------------");		
		TypeBussiness bussiness = new TypeBussiness();
		MonitorType monitorType = paddedMonitorType(actionForm);
		
		if(StringUtils.isEmpty(monitorType.getType())){
			error(req, resp, "类型名称不能为空");
			return;
		}
		OperationStatus status = bussiness.add(monitorType);
		
		switch (status) {
		case SUCCESS:
			redirect(req, resp, "/zebra/monitor/monitorType/monitorTypeAction");
			break;
		default:
			error(req,resp,"操作失败，请确认操作步骤是否正确！");
			break;
		}
	}
	
	/**
	 * 修改类型名称
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest req, 
						  HttpServletResponse resp, 
						  TypeActionForm actionForm)
		throws ServletException, 
			   IOException {
		logger.info("--------MonitorTypeAction.Update()------");
		TypeBussiness bussiness = new TypeBussiness();
		MonitorType monitorType = paddedMonitorType(actionForm);
		if(monitorType.getId() <= 0 ){
			error(req, resp, "更新操作失败，请确认数据是否完整！");
			return;
		}
		OperationStatus status = bussiness.update(monitorType);
		switch (status) {
		case SUCCESS:
			redirect(req, resp, "/zebra/monitor/monitorType/monitorTypeAction");
			break;
		default:
			error(req, resp, "更新操作失败，请确认操作步骤是否正确！");
			break;
		}
	}
	
	/**
	 * 删除操作
	 * <p>在进行删除操作时，必须确保任何站点、关键字属于该类别。</p>
	 * @param req
	 * @param resp
	 * @param actionForm 
	 * @throws ServletException
	 * @throws IOException
	 */
	private void del(HttpServletRequest req, 
					   HttpServletResponse resp, 
					   TypeActionForm actionForm)
		throws ServletException,
			   IOException {
		TypeBussiness bussiness = new TypeBussiness();
		MonitorType monitorType = paddedMonitorType(actionForm);
		OperationStatus status = bussiness.del(monitorType.getId());
		
		switch (status) {
		case SUCCESS:
			redirect(req, resp,"/zebra/monitor/monitorType/monitorTypeAction");
			break;
		case RELATIONSHIP_EMPTY:
			error(req, resp, "还有其他的引用关系关联到该类别，请将引用关系先删除掉！");
			break;
		default:
			error(req, resp, "删除操作失败，请确认操作步骤是否正确！");
			break;
		}
	}
	
	private void toUpdate(HttpServletRequest req, 
				 HttpServletResponse resp, 
				 TypeActionForm actionForm)
			throws ServletException, 
			 	   IOException {
		logger.info("--------MonitorTypeAction.toUpdate()------");
		TypeBussiness bussiness = new TypeBussiness();
		MonitorType monitorType = bussiness.get(actionForm.getId());
		if(monitorType != null) {
			req.setAttribute("monitorType", monitorType);
			forward(req, resp, "/zebra/monitor/monitorType/update.jsp");
			return;
		}else {
			error(req, resp, "没有找到相应的记录！");
		}
	}
	private void getAllObjectInThis(HttpServletRequest req, 
				 HttpServletResponse resp, 
				 TypeActionForm actionForm)
			throws ServletException, 
				   IOException {
		KeywordBussiness keywordBussiness = new KeywordBussiness();
		List<Keyword> keywordList = keywordBussiness.getAllKeywordInType(actionForm.getId());
		
		req.setAttribute("keywordList", keywordList);
		forward(req, resp, "/zebra/monitor/monitorType/innerList.jsp");
		
	}
	
	/**
	 * 将From中的值填充到MonitorType中
	 * @param actionForm 
	 * @return
	 */
	private MonitorType paddedMonitorType(TypeActionForm actionForm) {
		MonitorType monitorType = new MonitorType();
		monitorType.setId(actionForm.getId());
		monitorType.setType(actionForm.getType());
		monitorType.setIsStart(actionForm.getIsStart());
		return monitorType;
	}
}
